package com.salley.config;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.base.Predicate;

import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.ApiSelectorBuilder;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@ConditionalOnProperty(prefix = "swagger2",value = {"enable"}, havingValue = "true")
@EnableSwagger2
@Configuration
public class Swagger2Config {
	
	@Bean
	public ApiInfo apiInfo() {
		ApiInfoBuilder builder = new ApiInfoBuilder();
		builder.contact(new Contact("姓名", "url", "email"));
		builder.description("Spring session on redis");
		builder.title("Sesion 实现解析");
		builder.version("v1");
		return builder.build();
	}
	
	@Bean
	public Docket docket(@Qualifier ApiInfo apiInfo) {
		Docket docket = new Docket(DocumentationType.SWAGGER_2);
		docket.apiInfo(apiInfo);
		ApiSelectorBuilder select = docket.select();
		Predicate<RequestHandler> selector = RequestHandlerSelectors.withClassAnnotation(RestController.class);
		Predicate<RequestHandler> selector1 = RequestHandlerSelectors.withClassAnnotation(Controller.class);
		select.apis(selector);
		select.apis(selector1);
		Predicate<String> selector2 = PathSelectors.any();
		ApiSelectorBuilder paths = select.paths(selector2 );
		return paths.build();
	}
}
